package Impl;
import Util.JDBC;
import dao.replyDao;
import entity.ReplyInfo;
import entity.page.DetailPage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class ReplyDaoImpl  implements replyDao {
    TopicDaoImpl topicDao = new TopicDaoImpl();
    adminDaoImpl adminDao = new adminDaoImpl();
    @Override
    public ArrayList<DetailPage> getReplyInfoAndUserInfoById(Integer tid) {

        ArrayList<DetailPage> detailPages = new ArrayList<>();
        Connection con = JDBC.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;

        String sql = "select rTopic,rContents,rPublishTime,rModifyTime,rId,rUid,"
                + "(select uname from  user where user .uid = reply.ruid) as rName,"
                + "(select uFace from  user where user.uid = reply.ruid) as rFace,"
                + "(select uRegTime from  user where user.uid = reply.ruid) as rRegTime,"
                + "(select uType from  user where user.uid = reply.ruid) as rUserType,"
                + "(select uSex from  user where user.uid = reply.ruid) as rUsex"
                + " from reply where rtid in (?) order by rPublishTime";

        try
        {
            ps = con.prepareStatement(sql);
            Object[] param = {tid};
            rs = topicDao.query(ps, param,null,null);
            if (rs != null) {
                while (rs.next()) {
                    DetailPage temp = new DetailPage();
                    temp.setContents(rs.getString("rContents"));
                    temp.setFace(rs.getString("rFace"));
                    temp.setModifytime(rs.getTimestamp("rModifyTime"));
                    temp.setAuthor(rs.getString("rName"));
                    temp.setPublishtime(rs.getTimestamp("rPublishTime"));
                    temp.setRegtime(rs.getDate("rRegTime"));
                    temp.setTitle(rs.getString("rTopic"));
                    temp.setType(rs.getInt("rUserType"));
                    temp.setSex(rs.getBoolean("rUsex"));
                    temp.setId(rs.getInt("rId"));
                    temp.setUid(rs.getInt("rUid"));
                    detailPages.add(temp);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBC.closeAll(con,ps,rs);
        }
        return detailPages;
    }

    @Override
    public void insertReplyInfo(String title, String content, Integer tid, Integer sid, Integer uid){
        String sql = "insert into reply(rTid,rSid,rUid,rTopic,rContents,rPublishTime)"
                + " values(?,?,?,?,?,?)";
        Connection con = JDBC.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;

        content = content.replace("<", "&lt;");
        content = content.replace(">", "&gt;");
        content = content.replace(" ", "&nbsp");
        content = content.replace("\r\n", "<br>");
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        try {
//            ps = con.prepareStatement(sql);
            Object[] param = {tid, sid, uid, title, content, dateString};
            adminDao.commonUp(sql, param);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBC.closeAll(con,ps,rs);
        }
    }

    @Override
    public void getReplyCountById(Integer tid, Integer sid) {
        String sql = "update topicInfo set tReplyCount = "
                + " (select count(*) as replyCount from reply where rTid = ? and rSid = ?) "
                + " where tid = ?";
        Connection con = JDBC.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        Object[] param = {tid, sid, tid};
        adminDao.commonUp(sql, param);
        JDBC.closeAll(con,ps,rs);
    }

    @Override
    public void deleteReplyById(int rid) {
        String sql = "delete from reply where rid="+rid;
        adminDao.commonUp(sql);
        System.out.println("删除成功");
    }

    @Override
    public ArrayList<ReplyInfo> findReplyInfo(int rsid) {
        String sql = "select * from reply where rSid="+rsid;
        Connection con = JDBC.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        ArrayList<ReplyInfo> replyInfos = new ArrayList<>();
        try {
            ps = con.prepareStatement(sql);
            rs=ps.executeQuery();
            while (rs.next()){
                ReplyInfo tmp = new ReplyInfo();
                tmp.setRid(rs.getInt("rid"));
                tmp.setRtid(rs.getInt("rTid"));
                tmp.setRsid(rs.getInt("rSid"));
                tmp.setRuid(rs.getInt("rUid"));
                tmp.setRtopic(rs.getString("rTopic"));
                tmp.setRcontents(rs.getString("rContents"));
                tmp.setRpublishtime(rs.getString("rPublishTime"));
                tmp.setRmodifytime(rs.getString("rModifytime"));
                replyInfos.add(tmp);
            }
            System.out.println("查找回复信息成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBC.closeAll(con,ps,rs);
        }
        return replyInfos;

    }

    @Override
    public ArrayList<ReplyInfo> findAllReply() {
        String sql = "select * from reply";
        Connection con = JDBC.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        ArrayList<ReplyInfo> replyInfos = new ArrayList<>();
        try {
            ps = con.prepareStatement(sql);
            rs=ps.executeQuery();
            while (rs.next()){
                ReplyInfo tmp = new ReplyInfo();
                tmp.setRid(rs.getInt("rid"));
                tmp.setRtid(rs.getInt("rTid"));
                tmp.setRsid(rs.getInt("rSid"));
                tmp.setRuid(rs.getInt("rUid"));
                tmp.setRtopic(rs.getString("rTopic"));
                tmp.setRcontents(rs.getString("rContents"));
                tmp.setRpublishtime(rs.getString("rPublishTime"));
                tmp.setRmodifytime(rs.getString("rModifytime"));
                replyInfos.add(tmp);
            }
            System.out.println("查找回复信息成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBC.closeAll(con,ps,rs);
        }
        return replyInfos;
    }
}
